﻿using System;
using System.Data;
using DevExpress.Xpo.DB;
using KamLib.Converts;

namespace KamLib
{
    public class SessionDataReader : IDataReader
    {
        public SessionDataReader(SelectedData data)
        {
            Data = data;
            ResultSet = Data.ResultSet[0];
            RowIndex = -1;
        }

        public SelectedData Data { get; protected set; }
        public int ResultIndex { get; set; }
        public int RowIndex { get; set; }
        private SelectStatementResult ResultSet;
        private SelectStatementResultRow Row;

        public int Depth { get { throw new NotImplementedException("SessionDataReader.Depth"); } }
        public bool IsClosed { get { return false; } }
        public int RecordsAffected { get { return ResultSet.Rows.Length; } }

        public int FieldCount
        {
            get
            {
                return ResultSet.Rows.Length == 0 ? 0 : ResultSet.Rows[0].Values.Length;
            }
        }

        object IDataRecord.this[int i] { get { return GetValue(i); } }
        object IDataRecord.this[string name] { get { throw new NotImplementedException("SessionDataReader[]"); } }

        public void Close() { }
        public void Dispose() { }

        public bool NextResult()
        {
            if (ResultIndex >= Data.ResultSet.Length - 1) return false;
            ResultIndex++;
            ResultSet = Data.ResultSet[ResultIndex];
            RowIndex = -1;
            Row = null;
            return true;
        }

        public bool Read()
        {
            if (ResultSet == null || RowIndex >= ResultSet.Rows.Length - 1) return false;
            RowIndex++;
            Row = ResultSet.Rows[RowIndex];
            return true;
        }

        public string GetName(int i) { throw new NotImplementedException("SessionDataReader.GetName"); }
        public string GetDataTypeName(int i) { throw new NotImplementedException("SessionDataReader.GetDataTypeName"); }
        public Type GetFieldType(int i) { throw new NotImplementedException("SessionDataReader.GetFieldType"); }

        public object GetValue(int i) { return Row.Values[i]; }
        public int GetValues(object[] values) { throw new NotImplementedException("SessionDataReader.GetValues"); }
        public int GetOrdinal(string name) { throw new NotImplementedException("SessionDataReader.GetOrdinal"); }

        public bool GetBoolean(int i) { return GetValue(i).ToBool(); }
        public byte GetByte(int i) { return (byte)GetValue(i); }
        public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
        {
            throw new NotImplementedException("SessionDataReader.GetBytes");
        }
        public char GetChar(int i) { return (char)GetValue(i); }
        public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
        {
            throw new NotImplementedException("SessionDataReader.GetChars");
        }
        public Guid GetGuid(int i) { return (Guid)GetValue(i); }
        public short GetInt16(int i) { return (short)GetValue(i).ToInt(); }
        public int GetInt32(int i) { return GetValue(i).ToInt(); }
        public long GetInt64(int i) { return GetValue(i).ToInt64(); }
        public float GetFloat(int i) { return GetValue(i).ToFloat(); }
        public double GetDouble(int i) { return GetValue(i).ToDouble(); }
        public string GetString(int i) { return GetValue(i) + ""; }
        public decimal GetDecimal(int i) { return GetValue(i).ToDecimal(); }
        public DateTime GetDateTime(int i) { return GetValue(i).ToDateTime(); }
        public IDataReader GetData(int i) { throw new NotImplementedException("SessionDataReader.GetData"); }
        public bool IsDBNull(int i) { return Convert.IsDBNull(GetValue(i)); }

        public DataTable GetSchemaTable() { throw new NotImplementedException("SessionDataReader.GetSchemaTable"); }

    }
}
